package com.ssmr.demo.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    private static final String SSMR_EXCHANGE = "ssmr_exchange";
    private static final String SSMR_QUEUE = "ssmr_queue";
    private static final String SSMR_ROUTING_KEY = "ssmr_routing_key";


    /*https://docs.spring.io/spring-amqp/reference/htmlsingle/#_queue*/
    @Bean(name = "queue")
    public Queue queue() {
        return new Queue(SSMR_QUEUE, true, false, false);
    }

    /*https://juejin.im/post/59f194e06fb9a0451329ec53#heading-11*/
    @Bean(name = "directExchange")
    public DirectExchange exchange() {
        return new DirectExchange(SSMR_EXCHANGE);
    }

    @Bean
    public Binding binding(Queue queue, DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(SSMR_ROUTING_KEY);
    }


    /*https://www.jianshu.com/p/911d987b5f11*/
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

}
